Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
Context And Watch Expressions I have mentioned the term context a couple of times in relation to watch expressions. Context is closely related to the concept of variable scope that I covered in Chapter 5. To jog your memory, scope refers to the parts of a Visual Basic project in which a particular variable is visible or accessible. Likewise, the context of a watch expression determines the parts of the project where the expression is evaluated. Any watch expression’s context has three parts: Figure 26.3 Right-clicking on a watch expression displays this shortcut menu. • Procedure—Which procedure or procedures is the expression evaluated in? The default is the procedure that the variable is declared in (for local variables) or all procedures (for global variables). • Module—Which of the project modules will the expression be evaluated in? The default is the module where the term resides. In a multiple-module project, you can extend the context of a watch expression to all modules. • Project—The current project. This setting cannot be changed. Generally, you should leave the context settings at their default values for any watch expression. Extending an expression’s context beyond the default setting will slow program execution and should be done only when you have a specific reason to do so. When the program is in break mode and a watch expression displays <Out of context> in the Watches window, it simply means that the current break location is outside that expression’s context. This is rarely a problem; in parts of the program where a variable or property is out of context, it should not be contributing to any bugs. #### Quick Watches If you want to take a quick look at the value of a variable or expression, you can use a quick watch. While your program is in break mode, position the editing cursor on the variable or property name you are interested in or highlight the expression you want to evaluate. Then press Shift+F9 or select Quick Watch from the Debug menu. The Quick Watch dialog box is displayed, as shown in Figure 26.4. The parts of this dialog box are as follows: • Context—The current context (that is, the project, module, and procedure where execution is paused) • Expression—The selected variable or property name or expression • Value—The current value of the expression or <Out of context>, if appropriate While the Quick Watch dialog box is displayed, click on the Add button to add the expression to the Watches window. Click on Cancel to close the dialog box. Figure 26.4 The Quick Watch dialog box. TIP: Checking Values In break mode, you can rest the mouse cursor on a variable or property name in the Code Editing window to see a small, pop-up window with the current value displayed. #### Using The Immediate Window The Immediate window is automatically displayed in the Visual Basic environment when you run a program. It does not display anything at first, and in fact, it will remain blank if you do not use it. It is called the Immediate window, because any code you type into it is executed immediately. You can use the Immediate window whether or not a program is executing. To display it when no program is executing, select Immediate Window from the View menu or press Ctrl+G. When a program is not executing, the primary use for the Immediate window is to evaluate Basic expressions. If you type a line of Basic code in the Immediate window (or copy and paste it from the Code Editing window) and then press Enter, the code is executed immediately. Typically, you will use the Print statement to evaluate the value of expressions. Thus, if you enter
    print cos(.5)
and press Enter, Visual Basic will display 0.877582561890373 in the Immediate window. The Immediate window is much more useful in break mode, when program execution is paused. Then, you can view and change the value of any variables and properties that are available within the current context. For example, if execution is paused within a procedure that declares a variable named Total, then executing
    Print Total
in the Immediate window will display the current value of the variable. Executing
    Total = 10
will change the value of Total to 10. You are probably thinking you can accomplish these same tasks—viewing and changing a variable or property’s value—from the Watches window, and you are correct. The Immediate window, however, provides additional flexibility, permitting you to perform these tasks without cluttering the Watches window with too many expressions. Controlling Execution During Debugging The Debug menu has several commands that give you some control over the path of program execution. Two of these commands are used in break mode and are related to the next statement to be executed when execution resumes (by selecting Continue): • Show Next Statement—Moves the editing cursor to the next statement that will be executed when execution is continued. By default, this is the statement where the breakpoint is set. • Set Next Statement—Instructs Visual Basic that the next statement to be executed is the one containing the editing cursor. Why would you want to change the next statement? When the program is paused in break mode, you can use this command to move the next statement “back,” so that one or more statements immediately before the break location will be executed again. You can also move it ahead, so that one or more statements immediately following the break location will be skipped. Combined with changing variable values (using the Watches and Immediate windows, as described earlier), this technique can be powerful in helping you locate the program locations where bugs occur. The Run to Cursor command on the Debug menu can be useful in a similar manner. Selecting this command while in break mode causes execution to continue up to the line of code where the cursor is located. Execution will stop at any breakpoints encountered along the way, however. Single Stepping Your Program When debugging a program, you’ll find it useful at times to step through the program one line at a time. No, you do not have to set a breakpoint on every single line of code. Instead, use the Step commands from the Debug menu. These commands are listed in Table 26.2. You can see that Step Into and Step Over have the same effect if the next statement is not a procedure call. ### Distributing Your Program If the Visual Basic program you are writing is for your own use only, simply make an EXE file and place the executable in the appropriate folder on your hard disk. For most programs, however, you will need some way to distribute them to other users—from a few colleagues in your department to millions of customers worldwide. Fortunately, Visual Basic provides tools to simplify this task.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.